#include <iostream>
#include <map>
#include <string>
#include <math.h>

using std::cout;
using std::endl;
using std::string;
using std::map;
using std::to_string;

bool isHappyNum(int num)
{
    map<int,int> m;
    string strNum = "";
    int sum = -1;
    while(sum != 1)
    {
        sum = 0;
        strNum = to_string(num);
        for(auto &c : strNum)
        {
            sum += pow(std::stoi(string(1,c)),2);
        }
        num = sum;
        int count = m.count(sum);
        if(count != 0) return false;
        else
        {
            m[sum] = 1;
        }
    }
    return true;
}

int main(int argc, char const *argv[])
{
    // cout
    bool ret = isHappyNum(491);
    cout << "ret:" << ret << endl; 
    getchar();
    return 0;
}
